﻿using AutoMapper;
using MediatR;
using ProduceBasicData.Api.Application.Command.Material.BOM;
using ProduceBasicData.Api.DTO;
using ProduceBasicData.Domain.FactoryModeling;
using ProduceBasicData.Domain.Material;
using ProduceBasicData.ErrorCode;
using ProduceBasicData.Infrastructure.Interface;

namespace ProduceBasicData.Api.Application.Handler.Material.BOM
{
    public class SerachBOMHandler : IRequestHandler<SerachBOMCommand, ApiPaging<BOMDTO>>
    {
        private readonly IBaseRespository<BOMInfoModel> bomRepository;
        private readonly IBaseRespository<ProcessModel> processRepo;

        public SerachBOMHandler(IBaseRespository<BOMInfoModel> bomRepository, IBaseRespository<ProcessModel> processRepo)
        {
            this.bomRepository = bomRepository;
            this.processRepo = processRepo;
        }

        public Task<ApiPaging<BOMDTO>> Handle(SerachBOMCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<BOMDTO> res = new ApiPaging<BOMDTO>();

            var bom = bomRepository.GetAll().Where(x => !x.Isdel);
            var process = processRepo.GetAll().Where(x => !x.Isdel);

            // 应用筛选条件
            if (!string.IsNullOrEmpty(request.BOMName))
            {
                bom = bom.Where(x => x.BOMName.Contains(request.BOMName));
            }

            if (!string.IsNullOrEmpty(request.BomVersion))
            {
                bom = bom.Where(x => x.BomVersion.Contains(request.BomVersion));
            }

            if (!string.IsNullOrEmpty(request.BomType))
            {
                bom = bom.Where(x => x.BomType.Contains(request.BomType));
            }

            if (request.ProcessId.HasValue)
            {
                bom = bom.Where(x => x.ProcessId == request.ProcessId.Value);
            }

            // 如果只查询顶级BOM
            if (request.OnlyTopLevel)
            {
                bom = bom.Where(x => x.ParentBOMId == null);
            }

            var linq = from a in bom
                       join b in process on a.ProcessId equals b.Id
                       select new BOMDTO
                       {
                           Id = a.Id,
                           BOMDesc = a.BOMDesc,
                           BOMLevel = a.BOMLevel,
                           BOMName = a.BOMName,
                           BOMSteps = a.BOMSteps,
                           BomType = a.BomType,
                           BomVersion = a.BomVersion,
                           CreateName = a.CreateName,
                           CreateTime = a.CreateTime,
                           ProcessCode = b.Processcode,
                           ProcessId = a.ProcessId,
                           ProcessName = b.Processname,
                           UpdateName = a.UpdateName,
                           UpdateTime = a.UpdateTime,
                           BOMDosage = a.BOMDosage.HasValue ? a.BOMDosage.Value.ToString() : string.Empty,
                       };

            res.PageDatas = linq.OrderByDescending(x => x.CreateTime).Skip((request.Index - 1) * request.Size).Take(request.Size).ToList();
            res.TotalCount = linq.Count();
            res.Code = ApiEnum.Success;
            res.Msg = "BOM信息查询成功";

            return Task.FromResult(res);
        }
    }
}
